{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyPi9Xve396uxUAuTiWRDCBu",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sugarforever/LangChain-Tutorials/blob/main/LangChain_APIChain.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "j-ARiJc5771h",
        "outputId": "fad717a8-ed32-43c8-8ad5-3efa2fa9a87c"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m696.4/696.4 kB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.6/71.6 kB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m26.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 kB\u001b[0m \u001b[31m509.3 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -qU langchain openai"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.llms import OpenAI\n",
        "from langchain.chains import LLMRequestsChain, LLMChain"
      ],
      "metadata": {
        "id": "8fJjgh7C8TU7"
      },
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "llm = OpenAI(temperature=0, model_name='gpt-3.5-turbo', openai_api_key='your openai api key')"
      ],
      "metadata": {
        "id": "iInsE--I-yRY"
      },
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.prompts import PromptTemplate\n",
        "\n",
        "template = \"\"\"Between >>> and <<< are the raw search result text from provided RSS URL.\n",
        "Extract the answer to the question '{query}' or say \"not found\" if the information is not contained, and summarize all the information.\n",
        ">>> {requests_result} <<<\n",
        "Use the following JSON format to include all the titles:\n",
        "{{\n",
        "  \"titles\": [\n",
        "    'aaa',\n",
        "    'bbb',\n",
        "  ]\n",
        "}} \n",
        "Extracted:\"\"\"\n",
        "\n",
        "PROMPT = PromptTemplate(\n",
        "    input_variables=[\"query\", \"requests_result\"],\n",
        "    template=template,\n",
        ")"
      ],
      "metadata": {
        "id": "SczpN3-rA0_R"
      },
      "execution_count": 54,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "chain = LLMRequestsChain(llm_chain = LLMChain(llm=llm, prompt=PROMPT))"
      ],
      "metadata": {
        "id": "ZrYLB-e8A5I0"
      },
      "execution_count": 55,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "question = \"What are all the titles in this RSS feed?\"\n",
        "inputs = {\n",
        "    \"query\": question,\n",
        "    \"url\": \"https://rss.nytimes.com/services/xml/rss/nyt/US.xml\"\n",
        "}\n"
      ],
      "metadata": {
        "id": "Ts3Nc4otA66F"
      },
      "execution_count": 57,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response = chain(inputs)\n",
        "print(response['output'])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tXe0deYDA9Lt",
        "outputId": "0a251970-243e-4e19-8939-a518984742d9"
      },
      "execution_count": 58,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features=\"xml\"` into the BeautifulSoup constructor.\n",
            "  warnings.warn(\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\n",
            "  \"titles\": [\n",
            "    \"Minnesota Votes to Legalize Marijuana as Democrats Press Liberal Policies\",\n",
            "    \"Montana Governor Signs Law Banning Transgender Care for Minors\",\n",
            "    \"North Carolina Gerrymander Ruling Reflects Politicization of Judiciary Nationally\",\n",
            "    \"Colorado Governor Signs Bills Strengthening Gun Laws\",\n",
            "    \"Doorbell Prank Crash Trial: Anurag Chandra Is Found Guilty of Murder\",\n",
            "    \"New California Rule Would Ban Sale of Diesel Trucks by 2036\",\n",
            "    \"South Carolina Democrats Elect First Black Woman to Run State Party\",\n",
            "    \"Gunman Kills Five People in Texas and Is Still at Large, Officials Say\",\n",
            "    \"As Biden Runs for Re-election, Black Voters’ Frustration Bubbles\",\n",
            "    \"Army Grounds Nonessential Flights After 2 Helicopter Crashes\"\n",
            "  ]\n",
            "}\n",
            "\n",
            "Summary: The RSS feed contains news articles on various topics such as politics, law, crime, and environment. Some of the titles include Minnesota legalizing marijuana, Montana banning transgender care for minors, North Carolina's gerrymandering ruling, Colorado strengthening gun laws, and California banning diesel trucks by 2036. The feed also includes news on a doorbell prank crash trial, South Carolina electing its first black woman to run the state party, a shooting in Texas, and Black voters' frustration with President Biden. The Army has grounded nonessential flights after two helicopter crashes.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "template = \"\"\"在 >>> 和 <<< 之间是网页的返回的HTML内容。\n",
        "\n",
        "网页是新浪财经A股上市公司的每季度股东信息表格。\n",
        "\n",
        "请抽取参数请求的信息。每个截至日期作为JSON返回数据的date_of_quarter。因此，当表格中有多个截止日期时，返回数据应当包括所有的日期作为key。\n",
        "\n",
        ">>> {requests_result} <<<\n",
        "请使用如下的JSON格式返回数据\n",
        "{{\n",
        "  \"date_of_quarter\": [\n",
        "    {{\n",
        "      \"holder_name\": \"a\",\n",
        "      \"percentage\": \"50\"\n",
        "    }},\n",
        "    {{\n",
        "      \"holder_name\": \"b\",\n",
        "      \"percentage\": \"30\"\n",
        "    }},\n",
        "  ]\n",
        "}} \n",
        "\n",
        "例如，截至日期为2023-03-31，JSON数据应该是如下形式:\n",
        "\n",
        "{{\n",
        "  \"2023-03-31\": [\n",
        "    {{\n",
        "      \"holder_name\": \"a\",\n",
        "      \"percentage\": \"50\"\n",
        "    }},\n",
        "    {{\n",
        "      \"holder_name\": \"b\",\n",
        "      \"percentage\": \"30\"\n",
        "    }},\n",
        "  ]\n",
        "}}\n",
        "Extracted:\"\"\"\n",
        "\n",
        "PROMPT = PromptTemplate(\n",
        "    input_variables=[\"requests_result\"],\n",
        "    template=template,\n",
        ")"
      ],
      "metadata": {
        "id": "e9LFhyR0Ea9A"
      },
      "execution_count": 67,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "chain = LLMRequestsChain(llm_chain = LLMChain(llm=llm, prompt=PROMPT))"
      ],
      "metadata": {
        "id": "B8d9vOQXEdd7"
      },
      "execution_count": 68,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "inputs = {\n",
        "    \"url\": \"https://vip.stock.finance.sina.com.cn/corp/go.php/vCI_StockHolder/stockid/600519/displaytype/30.phtml\"\n",
        "}"
      ],
      "metadata": {
        "id": "u2kZVhqfEfCt"
      },
      "execution_count": 69,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response = chain(inputs)\n",
        "print(response['output'])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Y5_iZan9Eg8m",
        "outputId": "fb5fd134-7b8d-4d93-9b48-4affa785b261"
      },
      "execution_count": 70,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\n",
            "  \"2023-03-31\": [\n",
            "    {\n",
            "      \"holder_name\": \"中国贵州茅台酒厂(集团)有限责任公司\",\n",
            "      \"percentage\": \"54.06\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"香港中央结算有限公司\",\n",
            "      \"percentage\": \"7.26\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"贵州省国有资本运营有限责任公司\",\n",
            "      \"percentage\": \"4.54\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"贵州茅台酒厂(集团)技术开发有限公司\",\n",
            "      \"percentage\": \"2.22\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中央汇金资产管理有限责任公司\",\n",
            "      \"percentage\": \"0.83\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"深圳市金汇荣盛财富管理有限公司-金汇荣盛三号私募证券投资基金\",\n",
            "      \"percentage\": \"0.66\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"珠海市瑞丰汇邦资产管理有限公司-瑞丰汇邦三号私募证券投资基金\",\n",
            "      \"percentage\": \"0.65\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中国证券金融股份有限公司\",\n",
            "      \"percentage\": \"0.64\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中国工商银行-上证50交易型开放式指数证券投资基金\",\n",
            "      \"percentage\": \"0.39\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中国人寿保险股份有限公司-传统-普通保险产品-005L-CT001沪\",\n",
            "      \"percentage\": \"0.32\"\n",
            "    }\n",
            "  ],\n",
            "  \"2022-12-31\": [\n",
            "    {\n",
            "      \"holder_name\": \"中国贵州茅台酒厂(集团)有限责任公司\",\n",
            "      \"percentage\": \"54\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"香港中央结算有限公司\",\n",
            "      \"percentage\": \"6.75\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"贵州省国有资本运营有限责任公司\",\n",
            "      \"percentage\": \"4.54\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"贵州茅台酒厂(集团)技术开发有限公司\",\n",
            "      \"percentage\": \"2.21\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中央汇金资产管理有限责任公司\",\n",
            "      \"percentage\": \"0.83\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中国证券金融股份有限公司\",\n",
            "      \"percentage\": \"0.64\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"深圳市金汇荣盛财富管理有限公司－金汇荣盛三号私募证券投资基金\",\n",
            "      \"percentage\": \"0.61\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"珠海市瑞丰汇邦资产管理有限公司－瑞丰汇邦三号私募证券投资基金\",\n",
            "      \"percentage\": \"0.6\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中国银行股份有限公司－招商中证白酒指数分级证券投资基金\",\n",
            "      \"percentage\": \"0.43\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"中国工商银行－上证50交易型开放式指数证券投资基金\",\n",
            "      \"percentage\": \"0.43\"\n",
            "    }\n",
            "  ],\n",
            "  \"2022-09-30\": [\n",
            "    {\n",
            "      \"holder_name\": \"中国贵州茅台酒厂(集团)有限责任公司\",\n",
            "      \"percentage\": \"54\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"香港中央结算有限公司\",\n",
            "      \"percentage\": \"7.31\"\n",
            "    },\n",
            "    {\n",
            "      \"holder_name\": \"贵州省国有资本运营有限责任\n"
          ]
        }
      ]
    }
  ]
}